import { _decorator, Animation,AnimationClip, Component, Node, Sprite, SpriteFrame } from 'cc';
import { StateMachine, getInitParamsTrigger, getInitParamsNumber } from '../../Base/StateMachine';
import { PARAMS_NAME_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums';

import SpikesOneSubStateMachine from './SpikesOneSubStateMachine';
import SpikesFourSubStateMachine from './SpikesFourSubStateMachine';
import SpikesThreeSubStateMachine from './SpikesThreeSubStateMachine';
import SpikesTwoSubStateMachine from './SpikesTwoSubStateMachine';
import { SpikesManager } from './SpikesManager';




const { ccclass, property } = _decorator;



@ccclass('SpikesStateMachine')
export class SpikesStateMachine extends StateMachine {
    

    async init() {
        this.animationComponent = this.addComponent(Animation);

        this.initParams(); // 初始化参数
        this.initStateMachines(); // 初始化状态机
        this.initAnimationEvent();

        await Promise.all(this.waitingList)
    }

    initParams() {
        this.params.set(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT, getInitParamsNumber())
        this.params.set(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT, getInitParamsNumber())
    }

    initStateMachines(){
        this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_ONE, new SpikesOneSubStateMachine(this));
        this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_TWO, new SpikesTwoSubStateMachine(this));
        this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_THREE, new SpikesThreeSubStateMachine(this));
        this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_FOUR, new SpikesFourSubStateMachine(this));
        
    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED,()=> {
            const name = this.animationComponent.defaultClip.name;
            const { value } = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT);
            if(
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE && name.includes(`spikesone/two`))||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO && name.includes(`spikesone/three`)) || 
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE && name.includes(`spikesone/four`)) ||
                (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR && name.includes(`spikesone/five`))
            ) {
                this.node.getComponent(SpikesManager).backZero();
            }
        })
    }

    run() {
        const  param  = this.getParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT)
        const value = param?.value as number;
        switch (this.currentState) {
            case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE):
            case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO):
            case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE):
            case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR):
                if(value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_ONE) {
                    this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE)
                } else if(value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_TWO) {
                    this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO)
                } else if(value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_THREE) {
                    this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE)
                } else if(value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.SPIKES_FOUR) {
                    this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR)
                }else {
                    this.currentState = this.currentState
                }
                
                break;
        
            default:
                this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE)
                break;
        }
    }

}


